<template>
  <div class="register-container">
    <el-card class="register-card">
      <h1>欢迎注册 {{ projectStore.getProjectName }}</h1>
      <h2>注册</h2>

      <el-form
        :model="registerForm"
        :rules="registerRules"
        ref="registerFormRef"
        class="register-form"
        @submit.prevent="handleRegister"
        label-position="right"
        :label-width="labelWidth"
        :hide-required-asterisk="true"
      >
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            clearable
            show-password
          />
        </el-form-item>
        <el-form-item prop="realName" label="姓名">
          <el-input
            v-model="registerForm.realName"
            placeholder="请输入姓名"
            :prefix-icon="UserFilled"
            clearable
          />
        </el-form-item>
        <!-- 身份数据从 role.ts 中获取 -->
        <el-form-item prop="role" label="身份">
          <el-select v-model="registerForm.role" placeholder="请选择身份">
            <el-option
              v-for="role in roleStore.roles"
              :key="role.value"
              :label="role.label"
              :value="role.value"
              :disabled="role.disabled"
            />
          </el-select>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" :loading="loading" native-type="submit" style="width: 100%">
            注册
          </el-button>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-link type="primary" @click="$router.push('/login')"
            >已有账号？去登录</el-link
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/api/user';
import { useRoleStore } from '@/stores/project/role';
import { useProjectStore } from '@/stores/project/project';
import { User, Lock, UserFilled } from '@element-plus/icons-vue';

const router = useRouter();
const userStore = useUserStore();
const roleStore = useRoleStore();
const projectStore = useProjectStore();

const loading = ref(false);
const registerFormRef = ref(null);
const registerForm = ref({
  username: '',
  password: '',
  realName: '',
  role: '',
});

const labelWidth = computed(() => '60px');

const registerRules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  role: [{ required: true, message: '请选择身份', trigger: 'change' }],
});

const handleRegister = async () => {
  if (!registerFormRef.value) return;
  try {
    await registerFormRef.value.validate();
    loading.value = true;
    await userStore.register(registerForm.value);
    ElMessage.success('注册成功，请登录');
    router.push('/login');
  } catch (error) {
    ElMessage.error(error.message || '注册失败');
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.register-card {
  width: 400px;
  padding: 20px;
}

h1 {
  text-align: center;
  margin-bottom: 10px;
  font-size: 24px;
  color: #409eff;
}

h2 {
  text-align: center;
  margin-bottom: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-form-item__label {
  padding-right: 10px;
}
</style>